

/*
clean data for model calibration exercise

output:
- ../data/:
-- hqm_yields.dta: corporate bond indices (HQM Treasury)
-- macro_inflation_output_gap.dta: macro data
-- gsw_zc_yield_curve.dta: cleaned GSW yield data

- ../../model_nkph/data/targets/:
-- yield_macro_data.csv: combined yield and macro data
-- *habitat_regressions_*.csv: localization regression coefficients

*/



********************************************************************************
********************************************************************************
* clean fred data
n di "cleaning FRED model data"
* corporate bond indices (Treasury)
use ../data/fred/_fred_hqm_idxs.dta, clear
gen ym = ym(year(date), month(date))
format ym %tm
drop date*
order ym

forval y=1/30 {
	local m = 12*`y'
	rename HQMCB`y'YR ytil_`m'
	order ytil_`m', last
}

save ../data/hqm_yields.dta, replace


* macro data
use ../data/fred/_fred_macro.dta, clear
drop if missing(PCEPI)
gen ym = ym(year(date), month(date))
format ym %tm
drop date*
order ym
tsset ym

* inflation: yoy change
gen pi = 100*log(PCEPI / L12.PCEPI)

* cyclical component of IP
gen log_INDPRO = log(INDPRO)
tsfilter hp log_ip_gap = log_INDPRO , smooth(`=1600 * 3^4')
gen x = 100*log_ip_gap

keep pi x ym
save ../data/macro_inflation_output_gap.dta, replace




********************************************************************************
********************************************************************************
* clean NSS data
n di "cleaning NSS model data"
use ../data/input/gsw_nss_params.dta, clear
* collapse to monthly
gen ym = ym(year(date), month(date))
format ym %tm
drop date
order ym

collapse (lastnm) BETA0 BETA1 BETA2 BETA3 TAU1 TAU2, by(ym) fast


* compute zero-coupon yields for all maturities up to 30 years
* note: estimates for long-maturities are only valid for later in the sample
forval mat_m=6(6)360 {
	local mat_y = `mat_m' / 12
	calc_nss_yield, beta0(BETA0) beta1(BETA1) beta2(BETA2) beta3(BETA3) ///
		tau1(TAU1) tau2(TAU2) m(`mat_y') ///
		zeroname("y_`mat_m'")
}

keep ym y_*
save ../data/gsw_zc_yield_curve.dta, replace



********************************************************************************
********************************************************************************
* combined rate/macro data
n di "preparing model target data"


local target_dir ../../model_nkph/targets

use ../data/gsw_zc_yield_curve.dta, clear

* corporate bonds (high quality)
merge 1:1 ym using ../data/hqm_yields.dta, nogen

* output gap/inflation
merge 1:1 ym using ../data/macro_inflation_output_gap.dta, nogen

sort ym 
tsset ym

export delim `target_dir'/yield_macro_data.csv, delim(",") replace


* regression data (from empirical analysis)

* baseline localization
use ../data/rolling/phreg_baseline.dta, clear

gen tau = maturity_end - 1
order tau
sort tau
drop if tau <= 0
preserve
* non-crisis
rename D_star_S0 localization_short
rename D_star_L0 localization_long
keep tau localization_short localization_long
export delim `target_dir'/habitat_regressions_non_crisis.csv, delim(",") replace

restore
* crisis
rename D_star_S1 localization_short
rename D_star_L1 localization_long
keep tau localization_short localization_long
export delim `target_dir'/habitat_regressions_crisis.csv, delim(",") replace



* alternative localization
use ../data/rolling/phaltreg_D_b2c_all.dta, clear

gen tau = maturity_end - 1
order tau
sort tau
drop if tau <= 0
preserve
* non-crisis
rename B_shock_S0 alt_localization_short
rename B_shock_L0 alt_localization_long
keep tau alt_localization_short alt_localization_long
export delim `target_dir'/alt_habitat_regressions_non_crisis.csv, delim(",") replace

restore
* crisis
rename B_shock_S1 alt_localization_short
rename B_shock_L1 alt_localization_long
keep tau alt_localization_short alt_localization_long
export delim `target_dir'/alt_habitat_regressions_crisis.csv, delim(",") replace






